<?


class Controller {
	protected  $viewController;
	protected  $Mysql;
	protected  $idioma;
	protected  $admin;
	protected  static $baseUrl;

	function __construct($view, $admin = false) {
		$this->viewController = new $view();
		$this->Mysql = new Mysql();
		$this->idioma='es_ES';
		$this->admin = $admin;
		if($this->admin) { 
			require_once 'login_success.php';
		}
	}
	
	protected function getParam($param, $default = null) {
		$p = (isset($_GET[$param])? $_GET[$param] : $default);
		return preg_replace("/[^a-zA-Z0-9\s]/", '', $p);
	}
	public function dispatch() {
		
		$this->func = $this->getParam('func', 'index');
		$this->action = $this->getParam('action') . '_action';
		$this->execute();
	}
	protected function execute() {
		if(is_callable(array($this, $this->func . '_action'))) {
				$call = $this->func . '_action';
				$this->$call();
		}
		else {
			self::$baseUrl = (!$this->admin ? '/index.php' : '/panel/novedades.php');
			$this->redirect();
		}
	}
	
	protected function redirect($func = 'index', $error = null) {
		header('Location: ' . self::$baseUrl . '?func=' . $func . ($error ? '&error=' . $error : ''));
	}
}

?>

